package DAO;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import Beans.Config;
import Utils.HibernateUtil;

public class ConfigDao {
	private SessionFactory sessionFactory;

	public ConfigDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	Transaction tr = null;
	
	/**
	 * 删除配置
	 */
	public void delete(Config config) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(config);
		tr.commit();
		session.close();
	}
	
	/**
	 * 修改配置
	 */
	public void edit(Config config) {

		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(config);
		tr.commit();
		session.close();
	}
	
	/**
	 * 保存配置
	 */
	public void save(Config config) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.save(config);
		tr.commit();
		session.close();
	}
	
	/**
	 * 返回所有配置
	 */
	public List<Config> getAllconfig(){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(Config.class);
		List<Config> configs=c.list();
		tr.commit();
		session.close();
		return configs;
	}
	
	/**
	 * 通过配置ID获取配置
	 * 
	 * @return
	 */
	public Config getConfigById(int configId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Config config = (Config) session.get(Config.class, configId);
		tr.commit();
		session.close();
		return config;
	}
}
